#!/usr/bin/python
from numpy import *
from scipy import *
#from pylab import *

Meses=(["Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Set","Oct","Nov","Dic"])
 
Ene=linspace(0,30,31)
Feb=linspace(31,58,28)
Mar=linspace(59,89,31)
Abr=linspace(90,119,30)
May=linspace(120,150,31)
Jun=linspace(151,180,30)
Jul=linspace(181,211,31)
Ago=linspace(212,242,31)
Set=linspace(243,272,30)
Oct=linspace(273,303,31)
Nov=linspace(304,333,30)
Dic=linspace(334,364,31)

#Series de Markov
Bdm=([4205.2248410685615, 5681.4421887623712, 7679.7635742809007, 9623.0392501069891, 10991.639652730222, 11576.398473621346, 11290.54142379961, 10161.733916694191, 8406.6568199293179, 6363.4192023627966, 4606.1955824290635, 3772.7124960579708])
Ktdm=([ 0.47322083,0.46467075,  0.56251732,  0.55283989,  0.57134333,  0.62972953,  0.66161575,  0.65146362 , 0.60785162,  0.53430395 , 0.46893363 , 0.4559054 ])
Kddm=([0,0,0,0,0,0,0,0,0,0,0,0])
Kt_serie=zeros((59), dtype=float) 

Transicion_K=array([[0.000, 0.300, 0.350, 0.400, 0.450, 0.500, 0.550, 0.600, 0.650, 0.700],
  					[0.300, 0.350, 0.400, 0.450, 0.500, 0.550 ,0.600, 0.650, 0.700, 1.000],
					[0.031, 0.058, 0.051, 0.052, 0.028, 0.053, 0.044, 0.085, 0.010, 0.319],
					[0.705, 0.694, 0.753, 0.753, 0.807, 0.856, 0.818, 0.846, 0.842, 0.865]])

K_00_30=array([[0.229,0.333,0.208,0.042,0.083,0.042,0.042,0.021,0.000,0.000],
			 	[0.167,0.319,0.194,0.139,0.097,0.028,0.042,0.000,0.014,0.000],
				[0.250,0.250,0.091,0.136,0.091,0.046,0.046,0.023,0.068,0.000],
				[0.158,0.237,0.158,0.263,0.026,0.053,0.079,0.026,0.000,0.000],
				[0.211,0.053,0.211,0.158,0.053,0.053,0.158,0.105,0.000,0.000],
				[0.125,0.125,0.250,0.188,0.063,0.125,0.000,0.125,0.000,0.000],
				[0.040,0.240,0.080,0.120,0.080,0.080,0.120,0.120,0.080,0.040],
				[0.000,0.250,0.000,0.125,0.000,0.125,0.125,0.250,0.063,0.063],
				[0.000,0.250,0.000,0.125,0.250,0.000,0.250,0.000,0.000,0.125],
				[0.000,0.000,0.000,0.000,0.000,0.000,0.500,0.250,0.000,0.250]])

K_30_35= array([[0.000,0.000,0.091,0.000,0.364,0.091,0.182,0.000,0.273,0.000],
				[0.118,0.118,0.176,0.118,0.059,0.118,0.176,0.059,0.059,0.000],
				[0.067,0.267,0.067,0.200,0.067,0.000,0.133,0.133,0.000,0.067],
				[0.118,0.235,0.000,0.235,0.059,0.176,0.118,0.000,0.059,0.000],
				[0.077,0.154,0.308,0.077,0.154,0.077,0.000,0.077,0.077,0.000],
				[0.083,0.000,0.167,0.250,0.083,0.167,0.000,0.083,0.167,0.000],
				[0.222,0.222,0.000,0.111,0.111,0.000,0.111,0.222,0.000,0.000],
				[0.091,0.182,0.273,0.000,0.091,0.273,0.000,0.091,0.000,0.000],
				[0.111,0.111,0.111,0.222,0.000,0.000,0.000,0.222,0.111,0.111],
				[0.000,0.000,0.000,0.000,0.000,0.000,0.500,0.000,0.000,0.500]])

K_35_40=array([[0.206,0.088,0.176,0.176,0.088,0.029,0.176,0.029,0.029,0.000],
				[0.120,0.100,0.140,0.160,0.120,0.220,0.100,0.000,0.020,0.020],
				[0.077,0.123,0.185,0.123,0.077,0.139,0.092,0.123,0.061,0.000],
				[0.048,0.111,0.095,0.206,0.206,0.190,0.095,0.048,0.000,0.000],
				[0.059,0.137,0.118,0.137,0.098,0.118,0.118,0.157,0.059,0.000],
				[0.014,0.097,0.139,0.153,0.125,0.139,0.208,0.056,0.042,0.028],
				[0.073,0.101,0.116,0.145,0.087,0.159,0.203,0.087,0.029,0.000],
				[0.035,0.069,0.035,0.000,0.035,0.103,0.172,0.138,0.379,0.035],
				[0.000,0.167,0.167,0.000,0.167,0.000,0.000,0.333,0.000,0.167]])

K_40_45=array([[0.167,0.167,0.167,0.000,0.083,0.125,0.000,0.167,0.125,0.000],
				[0.117,0.117,0.150,0.117,0.083,0.117,0.200,0.067,0.017,0.017],
				[0.049,0.085,0.134,0.158,0.098,0.110,0.134,0.134,0.061,0.037],
				[0.039,0.090,0.141,0.141,0.167,0.141,0.090,0.141,0.039,0.013],
				[0.009,0.139,0.074,0.093,0.194,0.139,0.167,0.093,0.074,0.019],
				[0.036,0.018,0.117,0.099,0.144,0.180,0.180,0.117,0.072,0.036],
				[0.000,0.046,0.061,0.061,0.136,0.159,0.273,0.167,0.098,0.000],
				[0.016,0.056,0.080,0.128,0.104,0.080,0.160,0.208,0.136,0.032],
				[0.011,0.053,0.021,0.043,0.128,0.096,0.074,0.223,0.277,0.074],
				[0.000,0.074,0.037,0.000,0.074,0.074,0.074,0.074,0.033,0.259]])

K_45_50=array([[0.120,0.200,0.160,0.120,0.120,0.120,0.080,0.000,0.040,0.040],
				[0.100,0.080,0.120,0.140,0.140,0.200,0.180,0.040,0.000,0.000],
				[0.046,0.114,0.068,0.171,0.125,0.171,0.080,0.159,0.057,0.011],
				[0.015,0.061,0.084,0.099,0.191,0.153,0.153,0.115,0.115,0.015],
				[0.024,0.030,0.098,0.098,0.165,0.195,0.195,0.140,0.043,0.012],
				[0.015,0.026,0.062,0.124,0.144,0.170,0.170,0.222,0.062,0.005],
				[0.000,0.013,0.045,0.108,0.112,0.175,0.188,0.224,0.117,0.018],
				[0.008,0.023,0.054,0.066,0.093,0.125,0.191,0.253,0.183,0.004],
				[0.006,0.022,0.061,0.033,0.067,0.083,0.139,0.222,0.322,0.044],
				[0.000,0.046,0.091,0.091,0.046,0.046,0.136,0.091,0.273,0.182]])

K_50_55=array([[0.250,0.179,0.107,0.107,0.143,0.071,0.107,0.036,0.000,0.000],
				[0.133,0.022,0.089,0.111,0.156,0.178,0.111,0.133,0.067,0.000],
				[0.064,0.048,0.143,0.048,0.175,0.143,0.206,0.095,0.079,0.000],
				[0.000,0.022,0.078,0.111,0.156,0.156,0.244,0.167,0.044,0.022],
				[0.016,0.027,0.037,0.069,0.160,0.219,0.230,0.160,0.075,0.005],
				[0.013,0.025,0.030,0.093,0.144,0.202,0.215,0.219,0.055,0.004],
				[0.006,0.041,0.035,0.064,0.090,0.180,0.337,0.192,0.049,0.006],
				[0.012,0.021,0.029,0.035,0.132,0.123,0.184,0.371,0.082,0.012],
				[0.008,0.016,0.016,0.024,0.071,0.103,0.159,0.270,0.309,0.024],
				[0.000,0.000,0.000,0.000,0.059,0.000,0.059,0.294,0.412,0.176]])

K_55_60=array([[0.217, 0.087, 0.000, 0.174, 0.130, 0.087, 0.087, 0.130, 0.087, 0.000],
				[0.026, 0.079, 0.132, 0.079, 0.026, 0.158, 0.158, 0.132, 0.158, 0.053],
				[0.020, 0.020, 0.020, 0.040, 0.160, 0.180, 0.160, 0.200, 0.100, 0.100],
				[0.025, 0.013, 0.038, 0.076, 0.076, 0.139, 0.139, 0.266, 0.215, 0.013],
				[0.030, 0.030, 0.050, 0.020, 0.091, 0.131, 0.162, 0.283, 0.131, 0.071],
				[0.006, 0.006, 0.013, 0.057, 0.057, 0.121, 0.204, 0.287, 0.185, 0.064],
				[0.004, 0.026, 0.037, 0.030, 0.093, 0.107, 0.193, 0.307, 0.167, 0.037],
				[0.011, 0.009, 0.014, 0.042, 0.041, 0.071, 0.152, 0.418, 0.203, 0.041],
				[0.012, 0.022, 0.022, 0.038, 0.019, 0.050, 0.113, 0.281, 0.360, 0.084],
				[0.008, 0.024, 0.039, 0.039, 0.063, 0.039, 0.118, 0.118, 0.284, 0.268]])

K_60_65=array([[0.067, 0.133, 0.133, 0.067, 0.067, 0.200, 0.133, 0.133, 0.067, 0.000],
				[0.118, 0.059, 0.059, 0.059, 0.059, 0.118, 0.118, 0.235, 0.118, 0.059],
				[0.000, 0.024, 0.024, 0.049, 0.146, 0.073, 0.195, 0.244, 0.195, 0.049],
				[0.026, 0.000, 0.026, 0.026, 0.053, 0.184, 0.263, 0.184, 0.237, 0.000],
				[0.014, 0.000, 0.042, 0.056, 0.069, 0.097, 0.139, 0.306, 0.278, 0.000],
				[0.009, 0.009, 0.052, 0.069, 0.052, 0.112, 0.215, 0.285, 0.138, 0.060],
				[0.009, 0.009, 0.026, 0.017, 0.094, 0.099, 0.232, 0.283, 0.210, 0.021],
				[0.010, 0.014, 0.016, 0.019, 0.027, 0.062, 0.163, 0.467, 0.202, 0.019],
				[0.004, 0.007, 0.031, 0.017, 0.033, 0.050, 0.086, 0.252, 0.469, 0.050],
				[0.000, 0.000, 0.015, 0.046, 0.031, 0.046, 0.077, 0.123, 0.446, 0.215]])

#K_65_70=array([[
#K_70_00=array([[

#Selecionamos rango de la matriz y generamos Intervalo_K
 
Kt_serie[-1]= Ktdm[-1]

for mes in range(11):
	print mes
	#print range(2)
	for dia in range(len(eval(Meses[mes]))):
		
		K=Kt_serie[eval(Meses[mes])[dia]-1]
				
		
		for q in range(10):
			if Transicion_K[0,q] <= K <= Transicion_K[1,q]:
			 	Intervalo_K = linspace(Transicion_K[2,q], Transicion_K[3,q], 11)
	
		#calculamos la fila de trabajo 
		for p in range(11):
		    if p < 10:
			    if Intervalo_K[p]<=K<=Intervalo_K[p+1]:
    				Fil = p #	print Fil
    		else: Fil = 9
		

		if  K < 0.30:
			Matriz_trans=K_00_30 
		elif 0.30<=K<0.35 :
			Matriz_trans=K_30_35 
		elif   0.35<=K<0.40 : 
			Matriz_trans=K_35_40
		elif   0.40<=K<0.45 : 
			Matriz_trans=K_40_45
		elif   0.45<=K<0.50 : 
			Matriz_trans=K_45_50
		elif   0.50<=K<=0.55 : 
			Matriz_trans=K_50_55
		elif   0.55<=K<=0.60 : 
			Matriz_trans=K_55_60
		elif   0.60<=K : 
			Matriz_trans=K_60_65
			
		#Calculamos la columna

		Num_random = rand()
	
		for l in range(10):
			if Matriz_trans[Fil,:l].sum() > Num_random:
				Col = l ; print  Col
		#Interpolamos:
		#Los intervalos son: [ Intervalo_K[Fil],Intervalo_K[Fil+1] ] y [ Intervalo_K[Col],Intervalo_K[Col+1] ]
	
		#Kt_serie[eval(Meses[mes])[dia]]=(Intervalo_K[Col]+Intervalo_K[Col+1])/2 #opcion alternativa a interpolar ..Media
		#Kt_serie[eval(Meses[0][1])]= (Intervalo_K[Fil+1] -Intervalo_K[Fil])/(Intervalo_K[Col+1]-Intervalo_K[Col])*(K-Intervalo_K[Fil])+Intervalo_K[Col]  
	

print Kt_serie
